package org.zwy.gradetrack.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.zwy.gradetrack.common.Result;
import org.zwy.gradetrack.entity.OperationLog;
import org.zwy.gradetrack.service.OperationLogService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 操作日志控制器
 */
@RestController
@RequestMapping("/api/operation-logs")
public class OperationLogController {
    
    private final OperationLogService operationLogService;
    
    public OperationLogController(OperationLogService operationLogService) {
        this.operationLogService = operationLogService;
    }
    
    /**
     * 分页查询操作日志
     */
    @GetMapping("/page")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public Result<Page<OperationLog>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String operationModule,
            @RequestParam(required = false) String operationType,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        Page<OperationLog> page = new Page<>(current, size);
        return Result.success(operationLogService.pageLogs(page, operationModule, operationType, startTime, endTime));
    }
    
    /**
     * 创建操作日志
     */
    @PostMapping
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public Result<Boolean> create(@RequestBody OperationLog log) {
        try {
            return Result.success(operationLogService.createLog(log));
        } catch (RuntimeException e) {
            return Result.error(500, "创建操作日志失败：" + e.getMessage());
        }
    }
    
    /**
     * 批量创建操作日志
     */
    @PostMapping("/batch")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public Result<Boolean> batchCreate(@RequestBody List<OperationLog> logs) {
        try {
            return Result.success(operationLogService.batchCreateLogs(logs));
        } catch (RuntimeException e) {
            return Result.error(500, "批量创建操作日志失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据用户ID查询操作日志
     */
    @GetMapping("/user/{userId}")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public Result<List<OperationLog>> getByUserId(@PathVariable Long userId) {
        return Result.success(operationLogService.getLogsByUserId(userId));
    }
    
    /**
     * 根据操作类型查询操作日志
     */
    @GetMapping("/type/{operationType}")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public Result<List<OperationLog>> getByOperationType(@PathVariable String operationType) {
        return Result.success(operationLogService.getLogsByOperationType(operationType));
    }
    
    /**
     * 根据时间范围查询操作日志
     */
    @GetMapping("/time-range")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public Result<List<OperationLog>> getByTimeRange(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        return Result.success(operationLogService.getLogsByTimeRange(startTime, endTime));
    }
    
    /**
     * 清理指定时间之前的操作日志
     */
    @DeleteMapping("/clean")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public Result<Boolean> cleanLogs(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time) {
        try {
            return Result.success(operationLogService.cleanLogsBefore(time));
        } catch (RuntimeException e) {
            return Result.error(500, "清理操作日志失败：" + e.getMessage());
        }
    }
} 